﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlAgilityPack;
using WPViThanhNien.Supports.Models;
using System.Collections.Generic;
using System.Xml;

namespace WPViThanhNien.Supports.Adapters
{
    /// <summary>
    /// get data from internet
    /// </summary>
    public class DataAdapter
    {

        /// <summary>
        /// get detail content
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static string GetDetailHTMLContent(HtmlDocument document)
        {
            var rootNode = document.DocumentNode;
            var element = rootNode.SelectSingleNode(AdapterConstants.Detail_Content);

            return element.InnerHtml;
        }

        public static List<NewsModel> GetNewDataFromDocument(HtmlDocument document)
        {
            List<NewsModel> result = new List<NewsModel>(); // result list;

            var rootNode = document.DocumentNode;
            var elements = rootNode.SelectNodes(AdapterConstants.NewsPreview_ID_Panel_NewNews);

            foreach (var element in elements)
            {
                string[] temp = element.Attributes[AdapterConstants.NewsPreview_ID_Panel_NewNews_Attribute].Value.Split('\'');
                try
                {
                    NewsModel news = new NewsModel();
                    news.HotImageUrl = CreateImageUrl(temp[1]);
                    news.Title = temp[3];
                    news.HotContent = temp[5];
                    news.Url = AdapterConstants.NewsPreview_ParentUrl + element.Attributes["href"].Value;

                    result.Add(news);
                }
                catch { }
            }

            return result;
        }

        /// <summary>
        /// get list News Model from document
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static List<NewsModel> GetCatalogDataFromDocument(HtmlDocument document)
        {
            var result = new List<NewsModel>();

            var rootNode = document.DocumentNode;
            var elements = rootNode.SelectSingleNode(AdapterConstants.NewsPreview_ID_Panel_All);


            // get hosted new 
            var hostedElement = elements.SelectSingleNode(AdapterConstants.NewsPreview_ID_Panel_Hosted);
            result.Add(
                    GetDataFromElement(
                        hostedElement,
                        AdapterConstants.NewsPreview_ID_Panel_Hosted_Title,
                        AdapterConstants.NewsPreview_ID_Panel_Hosted_Image,
                        AdapterConstants.NewsPreview_ID_Panel_Hosted_Hot
                    ));

            //get normals news
            var normalNews = elements.SelectNodes(AdapterConstants.NewsPreview_ID_Panel_One);
            foreach (var ele in normalNews)
            {
                result.Add(GetDataFromElement(
                            ele,
                            AdapterConstants.NewsPreview_ID_Panel_One_Title,
                            AdapterConstants.NewsPreview_ID_Panel_One_Image,
                            AdapterConstants.NewsPreview_ID_Panel_One_Hot
                    ));
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="cmpTitle"></param>
        /// <param name="cmpImage"></param>
        /// <param name="cmpHot"></param>
        /// <returns></returns>
        public static NewsModel GetDataFromElement(HtmlNode node, String cmpTitle, String cmpImage, String cmpHot)
        {
            NewsModel result = new NewsModel();

            //get title and url
            var temp = node.SelectSingleNode(cmpTitle);
            result.Url = AdapterConstants.NewsPreview_ParentUrl + temp.Attributes["href"].Value;
            result.Title = temp.InnerText;

            //get image url
            temp = node.SelectSingleNode(cmpImage);
            result.HotImageUrl = CreateImageUrl(temp.Attributes["src"].Value);

            //get hot content
            temp = node.SelectSingleNode(cmpHot);
            result.HotContent = temp.InnerText;

            return result;
        }

        private static string CreateImageUrl(string input)
        {
            input = input.Substring(3, input.Length - 3);
            return Uri.EscapeUriString(AdapterConstants.NewsPreview_Image_ParentUrl + input);
        }
    }
}
